package com.xdcplus.web.service.wip.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xdcplus.mp.service.impl.BaseServiceImpl;
import com.xdcplus.web.common.pojo.dto.wip.WipUnpackBoxFilterDTO;
import com.xdcplus.web.common.pojo.entity.wip.WipUnpackBox;
import com.xdcplus.web.common.pojo.query.wip.WipUnpackBoxQuery;
import com.xdcplus.web.common.pojo.vo.wip.WipUnpackBoxVO;
import com.xdcplus.web.mapper.wip.WipUnpackBoxMapper;
import com.xdcplus.web.service.wip.WipUnpackBoxService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;

/**
 * 拆包料盒信息表(WipUnpackBox)表服务实现类
 *
 * @author bullion.yan
 * @since 2023-05-27 15:29:44
 */
@Slf4j
@Service("wipUnpackBoxService")
public class WipUnpackBoxServiceImpl extends BaseServiceImpl<WipUnpackBoxMapper, WipUnpackBox, WipUnpackBox, WipUnpackBoxVO> implements WipUnpackBoxService {

    @Autowired
    protected WipUnpackBoxMapper wipUnpackBoxMapper;

    @Override
    public Boolean saveOrUpdateBatch(List<WipUnpackBox> wipUnpackBoxList) {

        wipUnpackBoxList.forEach(wipUnpackBox -> {
            WipUnpackBox wipUnpackBoxParam = new WipUnpackBox();
            wipUnpackBoxParam.setId(wipUnpackBox.getId());
            if (ObjectUtil.isNotNull(wipUnpackBox.getId())) {
                wipUnpackBox.setId(wipUnpackBox.getId());
                wipUnpackBox.setUpdatedTime(new Timestamp(DateUtil.current()));
                LambdaUpdateWrapper<WipUnpackBox> lambdaUpdate = Wrappers.lambdaUpdate();
                lambdaUpdate.eq(WipUnpackBox::getId, wipUnpackBox.getId());
                update(wipUnpackBox, lambdaUpdate);
            } else {
                wipUnpackBox.setCreatedTime(new Timestamp(DateUtil.current()));
                save(wipUnpackBox);
            }
        });
        return true;
    }

    private List<WipUnpackBox> queryWipUnpackBoxList(WipUnpackBoxFilterDTO wipUnpackBoxFilterDTO) {
        WipUnpackBoxQuery wipUnpackBoxQuery = BeanUtil.copyProperties(wipUnpackBoxFilterDTO, WipUnpackBoxQuery.class);

        return wipUnpackBoxMapper.queryWipUnpackBox(wipUnpackBoxQuery);
    }

    @Override
    public List<WipUnpackBoxVO> queryWipUnpackBoxVOList(WipUnpackBoxFilterDTO wipUnpackBoxFilterDTO) {
        return this.objectConversion(queryWipUnpackBoxList(wipUnpackBoxFilterDTO));
    }

}
